<?php
namespace Home\Controller;
use Think\Controller;

class SendmsgboardcodeController extends  BaseController
{
	public function index()
	{
		if(app_conf("SMS_ON")==0)
		{
			$root['status'] = 0;
			$root['info'] = $GLOBALS['lang']['SMS_OFF'];
			output($root);
		}
		
				
		//检查用户,用户密码
		$user =  $GLOBALS['user_info'];
		$root['session_id'] = \es_session::id();
		$user_id  = intval($user['id']);
		$root['user_id'] = $user_id;
		if ($user_id >0){
			$user_mobile = strim($GLOBALS['request']['user_mobile']);
			
			if($user_mobile == '')
			{
				$root['status'] = 0;
				$root['info'] = $GLOBALS['lang']['VERIFY_MOBILE_EMPTY'];
				output($root);
			}
		
			if(!check_mobile($user_mobile))
			{
				$root['status'] = 0;
				$root['info'] = $GLOBALS['lang']['FILL_CORRECT_MOBILE_PHONE'];
				output($root);
			}
		
			if(!check_ipop_limit(CLIENT_IP,"register_mobile_verify",60,0))
			{
				$root['status'] = 0;
				$root['info'] = $GLOBALS['lang']['VERIFY_CODE_SEND_FAST'];
				output($root);
			}
		
		
			$begin_time = to_timespan(to_date(TIME_UTC,"Y-m-d"));
			
			if($GLOBALS['db']->getOne("SELECT send_count FROM ".DB_PREFIX."mobile_verify_code WHERE mobile='".$user_mobile."'  AND  create_time between ".$begin_time." and  ".($begin_time+24*3600)."") >= SEND_VERIFYSMS_LIMIT){
				$root['status'] = 0;
				$root['info'] = "你今天已经不能再发验证码了";
				output($root);
			}
			
			if($GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."mobile_verify_code WHERE client_ip='".CLIENT_IP."'  AND  create_time >=".(TIME_UTC - 60)."  ") > 0){
				$root['status'] = 0;
				$root['info'] = "请稍后再试";
				output($root);
			}
		
		
			//开始生成手机验证
			$verify_data['verify_code'] = rand(111111,999999);
			$verify_data['mobile'] = $user_mobile;
			$verify_data['create_time'] = TIME_UTC;
			$verify_data['client_ip'] = CLIENT_IP;
			$verify_data['send_count'] = 1;
			
			if($info = $GLOBALS['db']->getRow("SELECT * FROM ".DB_PREFIX."mobile_verify_code WHERE mobile='".$user_mobile."'")){
				if($info['create_time'] < $begin_time){
					$verify_data['send_count'] = 1;
				}
				else{
					$verify_data['send_count'] = $info['send_count'] + 1;
				}
				$GLOBALS['db']->autoExecute(DB_PREFIX."mobile_verify_code",$verify_data,"UPDATE","id=".$info['id']);	
			}
			else
				$GLOBALS['db']->autoExecute(DB_PREFIX."mobile_verify_code",$verify_data,"INSERT");
			
				send_verify_sms($user_mobile,$verify_data['verify_code'],$GLOBALS['user_info'],true);
				
				$result['status'] = 1;
				$root['response_code'] = $result['status'];			
				
				if ($root['response_code'] == 1){
					$root['show_err'] = $GLOBALS['lang']['MOBILE_VERIFY_SEND_OK'];
				}else{
					$root['show_err'] = $result['msg'];
					if ($root['show_err'] == null || $root['show_err'] == ''){
						$root['show_err'] = "验证码发送失败";
					}
				}
			
			
		}else{
			$root['response_code'] = 0;
			$root['show_err'] ="未登录";
			$root['user_login_status'] = 0;			
		}
		
		output($root);
	}
	
}
?>